package com.np.activitydemo2;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import com.np.activitydemo2.entity.ArticleEntity;
import com.np.activitydemo2.entity.BlogEntity;
import com.np.activitydemo2.utils.PermissionX;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class MainActivity extends AppCompatActivity {

    private static final String[] PERMISSIONS = new String[]{android.Manifest.permission.READ_CONTACTS};
    private static final int REQUEST_CODE = 1;

    private ActivityResultLauncher<Intent> mBlogResultLauncher;
    private ActivityResultLauncher<Intent> mArticleResultLauncher;
    private ActivityResultLauncher<Intent> mContactsResultLauncher;
    private TextView mViewLog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mViewLog = findViewById(R.id.view_log);

        findViewById(R.id.button_add_blog).setOnClickListener(v -> {
            mBlogResultLauncher.launch(new Intent(this, BlogAddActivity.class));
        });

        findViewById(R.id.button_add_article).setOnClickListener(v -> {
            mArticleResultLauncher.launch(new Intent(this, ArticleAddActivity.class));
        });

        findViewById(R.id.button_open_contacts).setOnClickListener(v -> {

            Uri uri = Uri.parse("geo:38.899533,-77.036476");
            Intent it = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(it);

//            PermissionX.checkPermission(this, PERMISSIONS, REQUEST_CODE);
//
//            Intent intent = new Intent();
//            intent.setAction("android.intent.action.PICK");
//            intent.addCategory("android.intent.category.DEFAULT");
//            intent.setType("vnd.android.cursor.dir/phone_v2");
//            mContactsResultLauncher.launch(intent);
        });

        mBlogResultLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
            @RequiresApi(api = Build.VERSION_CODES.O)
            @Override
            public void onActivityResult(ActivityResult result) {
                if (result != null && result.getResultCode() == RESULT_OK && result.getData() != null) {
                    BlogEntity entity = (BlogEntity) result.getData().getSerializableExtra("entity");
                    if (entity != null) {
                        String message = String.format("%s 用户添加了一条新的博客： %s\n", getCurrentDateTime("HH:mm:ss"), entity.getTitle());
                        mViewLog.append(message);
                    }
                }
            }
        });

        mArticleResultLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
            @RequiresApi(api = Build.VERSION_CODES.O)
            @Override
            public void onActivityResult(ActivityResult result) {
                if (result != null && result.getResultCode() == RESULT_OK && result.getData() != null) {
                    ArticleEntity entity = (ArticleEntity) result.getData().getSerializableExtra("entity");
                    if (entity != null) {
                        String message = String.format("%s 用户添加了一条新的文章： %s\n", getCurrentDateTime("HH:mm:ss"), entity.getTitle());
                        mViewLog.append(message);
                    }
                }
            }
        });

        mContactsResultLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
            @RequiresApi(api = Build.VERSION_CODES.O)
            @Override
            public void onActivityResult(ActivityResult result) {
                if (result != null && result.getResultCode() == RESULT_OK && result.getData() != null) {
                    ArticleEntity entity = (ArticleEntity) result.getData().getSerializableExtra("entity");
                    if (entity != null) {
                        String message = String.format("%s 用户添加了一条新的文章： %s\n", getCurrentDateTime("HH:mm:ss"), entity.getTitle());
                        mViewLog.append(message);
                    }
                }
            }
        });
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    private String getCurrentDateTime(String format) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
        return LocalDateTime.now().format(formatter);
    }
}